/** 
* @file        demo_gpio.c
*/

#include "zos_config.h"

#ifdef BOARD_USING_GPIO_DEMO
#include  "zos.h"

/************************************************
 * 
 * 中断测试函数
 * 
 * 
 *************************************************/
void gpio6_callback(void)
{
	zos_printf("this is pin 6 irq\r\n");
	zos_pin_irq_enable(PIN_6,PIN_IRQ_ENABLE);
}

void gpio7_callback(void)
{
	zos_printf("this is pin 7 irq\r\n");
	zos_pin_irq_enable(PIN_7,PIN_IRQ_ENABLE);
}

void gpio13_callback(void)
{
	zos_printf("this is pin 13 irq\r\n");
	zos_pin_irq_enable(PIN_13,PIN_IRQ_ENABLE);
}

 void hal_gpio_int_work_task(void)
 {
	zos_printf("hal_gpio_int_work_task runing\r\n");
	// zos_pin_attach_irq(PIN_6,PIN_IRQ_MODE_FALLING,gpio6_callback);
	// zos_pin_attach_irq(PIN_7,PIN_IRQ_MODE_HIGH_LEVEL,gpio7_callback);
	zos_pin_attach_irq(PIN_13,PIN_IRQ_MODE_RISING_FALLING,gpio13_callback);
	zos_pin_irq_enable(PIN_13,PIN_IRQ_ENABLE);
 	while(1)
 	{
 		zos_task_delay(1000);
 	}
 }


/************************************************
 * 
 * GPIO输入测试
 * 
 * 
void hal_gpio_in_work_task(void)
{
	zos_pin_mode(PIN_7,PIN_MODE_INPUT);
	while(1)
	{
		uint8_t gpio_in_value = 0xff;

		gpio_in_value = zos_pin_read(PIN_7);
		if(gpio_in_value == PIN_HIGH)
		{
			zos_printf("pin 7 high\r\n");
		}
		else if(gpio_in_value == PIN_LOW)
		{
			zos_printf("pin 7 low\r\n");
		}
		
		//通过zos_task_delay释放线程控制权
		zos_task_delay(1000);	
	}
}
*******************************************************/


int demo_gpio(int argc, char *argv[])
{
	zos_uint8_t pin_num;
	if(argc < 2)
	{
		zos_printf("Usage:\r\n");
		zos_printf("demo_gpio pin_num         - gpio demo   pin num(0-21)\r\n");
	}
	else
	{
		zos_printf("pin num is %s\r\n",argv[1]);
		pin_num = atoi(argv[1]);
		zos_printf("pin num is %d\r\n",pin_num);
		if(pin_num >PIN_MAX)
		{
			zos_printf("pin num is error\r\n");
		}
		else
		{
			zos_pin_mode(pin_num,PIN_MODE_OUTPUT);
			//隔3s切换GPIO引脚的输出状态，用于调试
			zos_pin_write(pin_num, PIN_HIGH);
			zos_printf("pin(%d) high\r\n",pin_num);
			//通过zos_task_delay()释放线程控制权
			zos_task_delay(2000);

			zos_pin_write(pin_num, PIN_LOW);
			zos_printf("pin(%d)) low\r\n",pin_num);
			//通过zos_task_delay()释放线程控制权
			zos_task_delay(2000);
		}
	}
	return ZOS_EOK;
}

#include "shell.h"

SHELL_EXPORT_CMD(demo_gpio,demo_gpio, gpio demo);
#endif //BOARD_USING_GPIO_DEMO
